package com.snow.common.controller;


import com.snow.common.annotation.Nfs;
import com.snow.common.config.MinioUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Objects;

/**
 * All rights Reserved, Designed By www.cusc.com
 *
 * @author yanke
 * @Title: Nfs
 * @Package com.snow.common.controller
 * @Description:类描述
 * @Email yanke@cu-sc.com
 * @version: v1.0.0
 * @date: 2021/7/1/0001 9:40
 * @Copyright: 2021  www.faw.com.cn Inc. All rights reserved. <br/>
 * Modification History:<br/>
 * Date         Author          Version            Description<br/>
 * *---------------------------------------------------------*<br/>
 * 2021/7/1/0001     Administrator           v1.0.0               修改原因<br/>
 * @since JDK 1.8
 */
@Slf4j
@RestController
@RequestMapping("/nfs")
@Nfs
public class NfsController {

    @Autowired(required = false)
    private MinioUtils minioUtils;

    @PostMapping("/upload")
    public void upload(@RequestParam(value = "file") MultipartFile multipartFile, String filePath) {
        // 会在本地产生临时文件，用完后需要删除
        try {
            String contentType = multipartFile.getContentType();
            minioUtils.uploadFile(null, multipartFile.getInputStream(), multipartFile.getOriginalFilename(), contentType);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("/uploadFile")
    public void upload(String filePath) {
        // 会在本地产生临时文件，用完后需要删除
        minioUtils.uploadFile(null, filePath);
    }

    @GetMapping("/presignedGetObject")
    public String presignedGetObject(@RequestParam("object") String object, @RequestParam(value = "expire", required = false) Integer expire) {
        if (Objects.nonNull(expire)) {
            return minioUtils.presignedGetObject(null, object, expire);
        } else {
            return minioUtils.presignedGetObject(null, object);
        }
    }
}
